package com.abe.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.abe.strengthtrainer.R;

public class CustomBallView extends View implements Runnable {

	protected Context mContext;
	/** 画笔 */
	private Paint mPaint;
	/** 画笔宽 */
	private int paintWidth;
	/** 画笔颜色 */
	private int paintColor;
	/** view高度 */
	protected float mHeight;
	/** view宽度 */
	protected float mWitdh;
	/** 小球直径 */
	private float ballWidth = 0;
	/** 小球圆心xy值 */
	/** x默认中间位置 */
	private float ballCenterX = 0;
	/** 动态Y */
	private float ballCenterY = 0;

	public CustomBallView(Context context) {
		this(context, null);
	}

	public CustomBallView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public CustomBallView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		paintColor = getResources().getColor(R.color.app_theme_color);
		/** 画笔宽度,并进行适配 */
		paintWidth = 2;
		mContext = context;
		init();
	}

	/**
	 * 由于onDraw()方法会不停的绘制 所以需要定义一个初始化画笔方法 避免导致不停创建造成内存消耗过大
	 */
	private void init() {
		mPaint = new Paint();
		setPaint();
	}

	/** 设置画笔 */
	private void setPaint() {
		/** 设置画笔为抗锯齿 */
		mPaint.setAntiAlias(true);
		/** 设置颜色 */
		mPaint.setColor(paintColor);
		/**
		 * 画笔样式分三种： 1.Paint.Style.STROKE：描边 2.Paint.Style.FILL_AND_STROKE：描边并填充
		 * 3.Paint.Style.FILL：填充
		 */
		mPaint.setStyle(Paint.Style.FILL);
		/**
		 * 设置描边的粗细，单位：像素px 注意：当setStrokeWidth(0)的时候描边宽度并不为0而是只占一个像素
		 */
		mPaint.setStrokeWidth(paintWidth);
	}

	@Override
	public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		mHeight = this.getMeasuredHeight();
		mWitdh = this.getMeasuredWidth();
		ballCenterX = mWitdh / 2;
		ballCenterY = mHeight / 2;
	}

	@Override
	public void onLayout(boolean changed, int left, int top, int right,
			int bottom) {
		super.onLayout(changed, left, top, right, bottom);
		/** 主要作用 ：为将整个根据子视图的大小以及布局参数将View树放到合适的位置上 */
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawCircle(ballCenterX, ballCenterY, ballWidth / 2, mPaint);
	}

	@Override
	public void run() {
		/** 通知刷新 */
		postInvalidate();
	}

	public float getBallWidth() {
		return ballWidth;
	}

	public void setBallWidth(float ballWidth) {
		this.ballWidth = ballWidth;
	}

	public float getBallCenterX() {
		return ballCenterX;
	}

	public void setBallCenterX(float ballCenterX) {
		this.ballCenterX = ballCenterX;
	}

	public float getBallCenterY() {
		return ballCenterY;
	}

	public void setBallCenterY(float ballCenterY) {
		this.ballCenterY = ballCenterY;
	}

	public int getPaintWidth() {
		return paintWidth;
	}

	public void setPaintWidth(int paintWidth) {
		this.paintWidth = paintWidth;
		setPaint();
	}

	public int getPaintColor() {
		return paintColor;
	}

	public void setPaintColor(int paintColor) {
		this.paintColor = paintColor;
		setPaint();
	}

}
